package com.comtime.mybox;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.comtime.login.LoginActivity;
import com.comtime.service.MyService;
import com.comtime.smartbox.BaseActivity;
import com.comtime.smartkilimanjaro.R;
import com.comtime.utils.MyConfig;
import com.comtime.utils.Util;
import com.comtime.view.InfoListView;
import com.comtime.view.MyDialog;
import com.comtime.view.MyProgressDialog;
import com.comyou.comyouhttp.ComyouHttpCallBack;
import com.comyou.comyouhttp.ComyouHttpClient;
import com.comyou.comyouhttp.ComyouHttpProgram;

public class FeedBackActivity extends BaseActivity {

	EditText edt_phone, edt_msg;
	InfoListView listView;
	int selete_position = -1;

	String[] nameValues = null;
	String[] infoValues = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feedback);

		edt_phone = (EditText) findViewById(R.id.edt_phone);
		edt_msg = (EditText) findViewById(R.id.edt_msg);
		init();
	}

	void init(){
		nameValues = new String[] { getText(R.string.help_one).toString(),
				getText(R.string.help_two).toString(),
				getText(R.string.help_three).toString(),
				getText(R.string.help_four).toString()
				};

		infoValues = new String[] { getText(R.string.help_one_info).toString(),
				getText(R.string.help_two_info).toString(),
				getText(R.string.help_three_info).toString(),
				getText(R.string.help_four_info).toString()
				 };
		listView =  (InfoListView) findViewById(R.id.listview);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				if (selete_position == position) {
					selete_position = -1;
				} else {
					selete_position = position;
				}
				adapter.notifyDataSetChanged();
			}
		});
	}
	
	public void backAction(View v) {
		this.finish();
		overridePendingTransition(R.anim.enter_left, R.anim.exit_right);
	}
	BaseAdapter adapter = new BaseAdapter() {

		@Override
		public View getView(int position, View view, ViewGroup arg2) {
			ViewHolder holder = null;
			if (view == null) {
				holder = new ViewHolder();
				view = LayoutInflater.from(FeedBackActivity.this).inflate(
						R.layout.item_help_info, null);
				holder.tv_name = (TextView) view.findViewById(R.id.tv_name);
				holder.tv_info = (TextView) view.findViewById(R.id.tv_info);
				holder.image_down = (ImageView) view
						.findViewById(R.id.image_down);
				holder.line_info = (LinearLayout) view
						.findViewById(R.id.line_info);
				view.setTag(holder);
			} else {
				holder = (ViewHolder) view.getTag();
			}
			holder.tv_name.setText(nameValues[position].toString());
			holder.tv_info.setText(infoValues[position].toString());
			if (selete_position == position) {
				holder.image_down.setImageResource(R.drawable.icon_top);
				holder.line_info.setVisibility(View.VISIBLE);
			} else {
				holder.image_down.setImageResource(R.drawable.icon_down);
				holder.line_info.setVisibility(View.GONE);
			}

			return view;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return nameValues == null ? 0 : nameValues.length;
		}
	};

	private class ViewHolder {
		TextView tv_name;
		TextView tv_info;
		ImageView image_down;
		LinearLayout line_info;
	}
	/**
	 * 如何添加箱包
	 * @param v
	 */
	public void HowAddboxAction(View v){
		selectQuestion(0);
	}
	/**
	 * 账号设置
	 * @param v
	 */
	public void HowSetboxAction(View v){
		selectQuestion(1);
	}
	/**
	 * 电池管理
	 * @param v
	 */
	public void HowSetBatteryAction(View v){
		selectQuestion(2);
	}
	/**
	 * 指纹管理
	 * @param v
	 */
	public void HowFingerAction(View v){
		selectQuestion(3);
	}
	void selectQuestion(int num){
		Intent intent=new Intent(this, QuestionAndAnswerActivity.class);
		intent.putExtra(MyService.VALUE_TAG, num);
		startActivity(intent);
	}
	
	/**
	 * 提交
	 * @param v
	 */
	public void CommitAction(View v) {
		if (!Util.hasNetwork(this)) {
			showOneDialog(getText(R.string.no_network).toString());
			return;
		}

		if (TextUtils.isEmpty(edt_msg.getText())) {
			showOneDialog(getText(R.string.feedback_hint).toString());
			return;
		}
		
		final MyProgressDialog progressDialog = new MyProgressDialog(
				FeedBackActivity.this);
		progressDialog.setContent(getText(R.string.commiting).toString());
		progressDialog.show();
		
		StringBuffer buffer=new StringBuffer();
		buffer.append(edt_msg.getText().toString());
		
		if(!TextUtils.isEmpty(edt_phone.getText())){
			buffer.append("\n"+getText(R.string.contact_type).toString()+":"+edt_phone.getText().toString());
		}
		
		ComyouHttpProgram httpProgram = new ComyouHttpProgram();
		httpProgram.add("subject", getText(R.string.feedback).toString());
		httpProgram.add("content", buffer.toString());
		ComyouHttpClient httpClient = new ComyouHttpClient(MyConfig.IP
				+ "sendmail.php");
		httpClient.postAsync(httpProgram, new ComyouHttpCallBack() {

			@Override
			public void onResponse(String string) {
				progressDialog.dismiss();
				Log.e("RZCJSON", "2 bind_success=============" + string);
				try {
					JSONObject jsonObject = new JSONObject(string);
					int err = jsonObject.getInt("err");
					if (err == 0) {
						handler.sendEmptyMessage(1);
					} else {
						handler.sendEmptyMessage(2);
					}

				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(String arg0, IOException arg1) {
				progressDialog.dismiss();
				handler.sendEmptyMessage(2);
			}
		});
	}

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				showOneDialog(getText(R.string.commit_success).toString());
				break;
			case 2:
				showOneDialog(getText(R.string.commit_fail).toString());
				break;
			default:
				break;
			}
		};
	};

	MyDialog myDialog;

	private void showOneDialog(String str) {

		if (myDialog != null) {
			myDialog.cancel();
		}
		myDialog = new MyDialog(FeedBackActivity.this);
		myDialog.setContent(str);
		myDialog.setLeftButtonText(getText(R.string.ok));
	
		myDialog.getLeftButton().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				myDialog.dismiss();

			}
		});
		myDialog.show();
	}
}
